package yxy.game.pm2.command.hero;

import yxy.cherry.data.bean.actcontrol;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_招募令礼包;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.Item;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.bean.pubsub.ActivityPublisher;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.HeroMessage;
import yxy.game.pm2.module.gacha.GachaHeroHandle;

/** 武将 抽武将界面 */
@MessageType(proto = Command.GachaInfo, classtype = HeroMessage.GachaInfoRequest.class)
public class CMD_GachaInfo extends Command<HeroMessage.GachaInfoRequest> {

	@Override
	public void execute(MessageSource messageSource, HeroMessage.GachaInfoRequest request) {

		try {
			String playerCode = messageSource.getTag();
			HeroMessage.GachaInfoResponse.Builder response = HeroMessage.GachaInfoResponse.newBuilder();

			//招募令礼包提示
			activityTip(playerCode);

			response.addAllInfo(new GachaHeroHandle().gachaInfo(playerCode));

			send(messageSource, response, Result.Success);
		} catch (Exception e) {
			e.printStackTrace();
		}


	}

	/**
	 * 招募令礼包提示
	 * @param playerCode
	 */
	private void activityTip(String playerCode) {
		try {
			PlayerBean playerBean = PlayerBean.get(playerCode);
			actcontrol actcontrol = ActDataCenter.getData(ActiveModule_招募令礼包.acttype, yxy.cherry.data.bean.actcontrol.class, playerCode);
			Currency currency = Item.get(playerCode, CurrencyType.CT_物品, 1002);
			if (actcontrol != null && currency.getNumber() < 10 && playerBean.getLevel() >= actcontrol.getGradecondition()){
				ActivityPublisher.getInstance().publish(playerCode, CCReason.招募武将, currency);  //招募令消息发布
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void send(MessageSource messageSource, HeroMessage.GachaInfoResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}